{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 处理百融重复信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2个成功 ----取竣工的，都竣工取时间最新 <br>\n",
    "2个失败 ----取时间最新的 <br>\n",
    "1个成功1个失败 ----   <br>\n",
    "成功失败分开处理重复，然后合并到一起处理一个成功一个失败的<br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-12T07:53:00.441268Z",
     "start_time": "2019-08-12T07:52:44.906368Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success!\n"
     ]
    }
   ],
   "source": [
    "# 导入百融csv文件\n",
    "import pandas as pd\n",
    "import os\n",
    "os.chdir('D:\\百融去重处理')\n",
    "br=pd.read_csv('bairong812.csv',low_memory=False)\n",
    "# br\n",
    "# 查看列名\n",
    "# br.columns\n",
    "\n",
    "# 根据指定列名取出重复行并查看\n",
    "#br_duplicated = br[br.duplicated('CERT_NO_CIPHERTEXT',keep=False)]\n",
    "# 排序\n",
    "#br_duplicated_sort = br_duplicated.sort_values(['REQUEST_DATE'],ascending=0,inplace=False)\n",
    "# br_duplicated_sort\n",
    "\n",
    "# br_duplicated_sort.groupby(['CERT_NO_CIPHERTEXT','CUSTOMER_NAME']).count().sort_values(['ACCOUNT_ID'],ascending=0)\n",
    "# br_duplicated_sort.loc[br_duplicated_sort['STATUS']=='SUCCESS']\n",
    "\n",
    "# 把数据分为成功和失败\n",
    "success = br[br['STATUS']=='SUCCESS']\n",
    "fail    = br[br['STATUS']=='FAIL']\n",
    "\n",
    "# 处理成功部分\n",
    "success_sort = success.sort_values(['CERT_NO_CIPHERTEXT','ACCOUNT_ID1','REQUEST_DATE'],ascending=[0,0,0],inplace=False)\n",
    "# 删除重复项，保留重复项第一行\n",
    "success_last = success_sort.drop_duplicates(subset=['CERT_NO_CIPHERTEXT'],keep='first',inplace=False)\n",
    "\n",
    "\n",
    "# 处理失败部分\n",
    "fail_sort = fail.sort_values(['CERT_NO_CIPHERTEXT','REQUEST_DATE'],ascending=[0,0],inplace=False)\n",
    "# 删除重复项，保留重复项第一行\n",
    "fail_last = fail_sort.drop_duplicates(subset=['CERT_NO_CIPHERTEXT'],keep='first',inplace=False)\n",
    "\n",
    "# 合并得到重复行只有一个成功一个失败（取成功的）\n",
    "different = pd.concat([success_last,fail_last])\n",
    "different_sort = different.sort_values(['CERT_NO_CIPHERTEXT','STATUS'],ascending=[0,0],inplace=False)\n",
    "# 删除重复项，保留重复项第一行(status为success) (根据status排序，success在上面)\n",
    "br_last  = different_sort.drop_duplicates(subset=['CERT_NO_CIPHERTEXT'],keep='first',inplace=False)\n",
    "br_last1 = br_last.drop(columns=['ACCOUNT_ID1'])\n",
    "br_last1.to_csv('去重百融_812.csv',encoding='utf_8_sig')\n",
    "print('Success!')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
